/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.        */
/*                                                                 */
/*  NAME      = test_sip.c                                        */
/*  NOTE      =                                                    */
/*  DATE      = 2016/02/16 by songbb                               */
/*******************************************************************/

#include <stdio.h>
#include <assert.h>
#include <CUnit/Basic.h>
#include <CUnit/CUnit.h>
#include <module.h>

#include "apmtest_cunit.h"
#include "frcomm.h"
#include "frmem.h"
#include "../../src/module/sip/config.h"
#include "t_sip_header/t_sip_analyze_header.h"
#include "t_sip_comm/t_sip_comm.h"
#include "t_sip_core/t_sip_core.h"
#include "t_sip_packet/t_sip_packet.h"
#include "t_sip_req/t_sip_req.h"
#include "t_sip_resp/t_sip_resp.h"

static Frmemstat_mng g_st_memstat_mng = {0, FR_MEMSTAT_COUNT_MAX};
static pthread_rwlock_t g_memstat_rwlock = PTHREAD_RWLOCK_INITIALIZER;
static Frmem_env g_st_mem_env = {&g_st_memstat_mng, &g_memstat_rwlock};

FR_STATIC Frexp_ref g_st_exp_ref = {NULL, NULL};
FR_STATIC Frservice_ref g_st_service_Ref = {NULL, NULL};
ApmContext g_st_apm_context = {&g_st_exp_ref, NULL, &g_st_service_Ref, NULL, NULL, &g_st_mem_env};
void *g_p_apm_context = NULL;

int main(int argc, char **argv)
{
    if (fr_slab_init(&g_st_mem_env, MODULE_NAME) == NULL)
        return FR_FAIL;

    apmtest_cunit_registry();

    FunType fun_suites[1024];
    memset(fun_suites, 0x00, sizeof(fun_suites));
    int i_suites_len = sizeof(fun_suites) / sizeof(FunType);
    int i_loop = 0;

    // 加载各个测试组件
    // t_sip_comm
    apmtest_cunit_add_suites(fun_suites, i_suites_len, add_suite_sip_free);
    apmtest_cunit_add_suites(fun_suites, i_suites_len, add_suite__sip_frtime2datetimestr);
    apmtest_cunit_add_suites(fun_suites, i_suites_len, add_suite_sip_bin_to_hex_string);
    apmtest_cunit_add_suites(fun_suites, i_suites_len, add_suite_sip_packet_is_in_fragment);
    apmtest_cunit_add_suites(fun_suites, i_suites_len, add_suite_sip_report_data);
    apmtest_cunit_add_suites(fun_suites, i_suites_len, add_suite_sip_free_stmt);

    // t_sip_core
    apmtest_cunit_add_suites(fun_suites, i_suites_len, add_suite__sip_add_error_nums);
    apmtest_cunit_add_suites(fun_suites, i_suites_len, add_suite_sip_core_add_unfinshed_fragmented_packets);
    apmtest_cunit_add_suites(fun_suites, i_suites_len, add_suite__sip_private_session_init);
    apmtest_cunit_add_suites(fun_suites, i_suites_len, add_suite__sip_analyze_direct);
    apmtest_cunit_add_suites(fun_suites, i_suites_len, add_suite_sip_detect);
    apmtest_cunit_add_suites(fun_suites, i_suites_len, add_suite__sip_destroy_session);
    apmtest_cunit_add_suites(fun_suites, i_suites_len, add_suite__sip_destroy_packet);
    apmtest_cunit_add_suites(fun_suites, i_suites_len, add_suite__sip_destroy_stmt);
    apmtest_cunit_add_suites(fun_suites, i_suites_len, add_suite_sip_session_destroy);
    apmtest_cunit_add_suites(fun_suites, i_suites_len, add_suite_sip_analyze);

    // t_sip_header
    apmtest_cunit_add_suites(fun_suites, i_suites_len, add_suite_sip_analyze_header);

    // t_sip_packet
    apmtest_cunit_add_suites(fun_suites, i_suites_len, add_suite_sip_init_packet);
    apmtest_cunit_add_suites(fun_suites, i_suites_len, add_suite__sip_pkt_is_fragmented);
    apmtest_cunit_add_suites(fun_suites, i_suites_len, add_suite__sip_packet_check_fragment);
    apmtest_cunit_add_suites(fun_suites, i_suites_len, add_suite_sip_packet_unfragment);

    // t_sip_req
    apmtest_cunit_add_suites(fun_suites, i_suites_len, add_suite__sip_unpack_req);
    apmtest_cunit_add_suites(fun_suites, i_suites_len, add_suite_sip_analyze_req);

    // t_sip_resp
    apmtest_cunit_add_suites(fun_suites, i_suites_len, add_suite__sip_unpack_resp);
    apmtest_cunit_add_suites(fun_suites, i_suites_len, add_suite_sip_analyze_resp);

    for (;i_loop < i_suites_len; i_loop++)
    {
        if (fun_suites[i_loop] != NULL && fun_suites[i_loop]() != 0)
        {
            CU_cleanup_registry();
            return CU_get_error();
        }
    }

    apmtest_cunit_unregistry();

    fr_slab_destroy();

    return 0;
}
